package os.filesystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import os.filesystem.exception.FSException;
import os.filesystem.tool.Command;
import os.filesystem.tool.FSTool;
import os.filesystem.tool.ToolFactory;
import os.filesystem.utils.FSProperties;

public class FSMain {

	/**
	 * Main method.
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		FSBootstrap.bootstrap();
		// command prompt
		String cmdPrompt = FSProperties.getProperty(FSProperties.USER_PROMPT);
		System.out.println("Chumbo FS has started!");

		while (true) {
			System.out.print(cmdPrompt);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			String line = br.readLine();
			if (!line.trim().isEmpty()) {
				try {
					if (line.equals("exit")) {
						break;
					}

					Command command = CommandLineParser.parse(line);
					FSTool tool = ToolFactory.getTool(command
							.getCommandNameArgument().getArgument());
					if (tool != null) {
						tool.execute(command);
					} else {
						continue;
					}
				} catch (FSException e) {
					System.out.println(e.getMessage());
				}
			}
		}
		FSBootstrap.unstrap();
		System.out.println("Good bye!");
	}
}
